package com.tao.com.xiaowen.config.http;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tao.com.xiaowen.model.BmobUserAuth;
import com.tao.com.xiaowen.model.JokeData;
import com.tao.com.xiaowen.model.News;
import com.tao.com.xiaowen.model.Picture;
import com.tao.com.xiaowen.model.UserInfos;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by Administrator on 2016/8/3.
 */
public class MyJson {
    //解析笑话方法
    public static List<JokeData> getJokeJsonList(String result) {
        List<JokeData> jokeList = null;
        Gson gson = new Gson();
        Type type = new TypeToken<List<JokeData>>() {
        }.getType();
        try {
            JSONObject jsonObject1 = new JSONObject(result);
            JSONObject jsonObject = jsonObject1.getJSONObject("result");
            String data = jsonObject.getJSONArray("data").toString();
            jokeList = gson.fromJson(data, type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jokeList;
    }


    public static List<News> getNewsJsonList(String result) {
        List<News> newsList = null;
        Gson gson = new Gson();
        Type type = new TypeToken<List<News>>() {
        }.getType();
        try {
            JSONObject jsonObject1 = new JSONObject(result);
            JSONObject jsonObject = jsonObject1.getJSONObject("result");
            String data = jsonObject.getJSONArray("data").toString();
            newsList = gson.fromJson(data, type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return newsList;
    }

    public static List<Picture> getPictureJsonList(String result) {
        List<Picture> pictureList = null;
        Gson gson = new Gson();
        Type type = new TypeToken<List<Picture>>() {
        }.getType();
        try {
            JSONObject jsonObject = new JSONObject(result);
            String data = jsonObject.getJSONArray("data").toString();
            pictureList = gson.fromJson(data, type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return pictureList;
    }

    public static BmobUserAuth getUserAuth(Object object) {
        BmobUserAuth userAuth = null;
        Gson gson = new Gson();
        String jsonString = gson.toJson(object);
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String data = jsonObject.getJSONObject("nameValuePairs").toString();
            userAuth = gson.fromJson(data, BmobUserAuth.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return userAuth;
    }

    public static BmobUserAuth getBmobUserAuth(Object object) {
        BmobUserAuth userAuth = null;
        Gson gson = new Gson();
        String jsonString = gson.toJson(object);
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONObject jsonObject1 = jsonObject.getJSONObject("nameValuePairs");
            JSONObject jsonObject2 = jsonObject1.getJSONObject("qq");
            String data = jsonObject2.getJSONObject("nameValuePairs").toString();
            userAuth = gson.fromJson(data, BmobUserAuth.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return userAuth;
    }

    public static UserInfos getUserInfoJson(Object object) {
        UserInfos userInfos = null;
        Gson gson = new Gson();
        String jsonString = gson.toJson(object);
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String data = jsonObject.getJSONObject("nameValuePairs").toString();
            userInfos = gson.fromJson(data, UserInfos.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return userInfos;
    }

}
